﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class hotel_bll : base_dll
    {
        public List<Wjtour.Domain.hotel> GetList(int top)
        {
            string sql = string.Format("select top {0} * from hotel order by isrecommend desc, id desc", top);
            return GetAll<Wjtour.Domain.hotel>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.hotel> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and hotelname like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from hotel 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.hotel> data = GetPaginationData<Wjtour.Domain.hotel>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.hotel Load(int id)
        {
            string sql = "select * from hotel where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.hotel>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.hotel entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [hotel] SET 
                [hotelname] = @hotelname,
                [address] = @address,
                [opentime] = @opentime,
                [decrocatetime] = @decrocatetime,
                [reservephone] = @reservephone,
                [averageprice] = @averageprice,
                [hotelstar] = @hotelstar,
                [trafficguide] = @trafficguide,
                [introduce] = @introduce,
                [img] = @img,
                [addtime] = @addtime,
                isrecommend = @isrecommend,
                web = @web
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);
            }
            else
            {
                sql = @"INSERT INTO [hotel]( 
                [hotelname],[address],[opentime],[decrocatetime],[reservephone],[averageprice],[hotelstar],[trafficguide],[introduce],[img],[addtime],isrecommend,web)VALUES(
                @hotelname,@address,@opentime,@decrocatetime,@reservephone,@averageprice,@hotelstar,@trafficguide,@introduce,@img,@addtime,@isrecommend,@web)";
            }
            DbAccess.AddInParameter(cmd, "@hotelname", DbType.String, entity.hotelname);
            DbAccess.AddInParameter(cmd, "@address", DbType.String, entity.address);
            DbAccess.AddInParameter(cmd, "@opentime", DbType.String, entity.opentime);
            DbAccess.AddInParameter(cmd, "@decrocatetime", DbType.String, entity.decrocatetime);
            DbAccess.AddInParameter(cmd, "@reservephone", DbType.String, entity.reservephone);
            DbAccess.AddInParameter(cmd, "@averageprice", DbType.Decimal, entity.averageprice);
            DbAccess.AddInParameter(cmd, "@hotelstar", DbType.Int32, entity.hotelstar);
            DbAccess.AddInParameter(cmd, "@trafficguide", DbType.String, entity.trafficguide);
            DbAccess.AddInParameter(cmd, "@introduce", DbType.String, entity.introduce);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@isrecommend", DbType.Int32, entity.isrecommend);
            DbAccess.AddInParameter(cmd, "@web", DbType.String, entity.web);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from hotel where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
